package edu.fivegroup.mall.consumer;


import edu.fivegroup.mall.goods.Goods;
import edu.fivegroup.mall.util.ConnectionDatabaseForMySQLUseJdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConsumerDaoImpl implements ConsumerDao {
    private Connection connection = null;
    private PreparedStatement pstmt = null;
    private ResultSet rset = null;

    public ConsumerDaoImpl() {
        connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
    }

    @Override
    public List<Goods> getAllGoods() {
        List<Goods> list = new ArrayList<>();
        try{
            String sql = "SELECT id, title, price, descr, img, state, mid " +
                        "FROM t_goods " +
                        "WHERE state = 0";
            pstmt = connection.prepareStatement(sql);


            rset = pstmt.executeQuery();
            while (rset.next()) {
                Goods goods = new Goods(
                        rset.getInt("id"),
                        rset.getString("title"),
                        rset.getBigDecimal("price"),
                        rset.getString("descr"),
                        rset.getString("img"),
                        rset.getInt("state"),
                        rset.getInt("mid")
                );
                list.add(goods);
            }
            return list;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Consumer login(String username, String password) {
        Consumer consumer = null;
        try {
            pstmt = connection.prepareStatement("select * from t_consumer where account = ? and  password = ?");
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            rset = pstmt.executeQuery();
            if (rset.next()){
                consumer = new Consumer(
                        rset.getInt("id"),
                        rset.getInt("age"),
                        rset.getInt("state"),
                        rset.getString("account"),
                        rset.getString("password"),
                        rset.getString("realname"),
                        rset.getString("idcard"),
                        rset.getString("tel"),
                        rset.getString("province"),
                        rset.getString("city"),
                        rset.getString("district"),
                        rset.getString("addr")
                );
            }
            return consumer;
        }catch (Exception e){
            e.printStackTrace();

        }
        return null;

    }

    @Override
    public int register(String username, String password) {
        Consumer consumer = null;
        try {
            // 检查用户名是否已存在
            pstmt = connection.prepareStatement("select * from t_consumer where account = ?");
            pstmt.setString(1,username);
            rset = pstmt.executeQuery();
            if (rset.next()){
                System.out.println(rset.getString("account") + "已存在");
                return 0;
            }
            // 插入用户信息
            pstmt = connection.prepareStatement("insert into t_consumer(account,password) values(?,?)");
            pstmt.setString(1, username);
            pstmt.setString(2, password);

            int i = pstmt.executeUpdate();
            return i == 1 ? 1 : 0;      //三目运算符
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;

    }

}
